#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>


typedef struct Sendusermsg
{
	int code;
	char name[32];
	char password[32];
	char sex;
	int age;
}sendusermsg;
int main(int argc, const char *argv[])
{
	int sockfd=socket(AF_INET,SOCK_STREAM,0);

	struct sockaddr_in serveraddr;
	serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=htons(atoi(argv[2]));	
	
	socklen_t len=sizeof(serveraddr);
	connect(sockfd,(struct sockaddr *)&serveraddr,len);
	int choose1=0;
	int choose2=0;
	int choose2flag=0;
	sendusermsg msg;
	char recvbuf[128]={0};
	char username[32]={0};
windows1:
	while(1)
	{
		printf("**************************\n");
		printf("***1.register**2.login***\n");
		printf("*************************\n");
		scanf("%d",&choose1);
		getchar();
		switch(choose1)
		{
			case 1:
				msg.code=1;
				printf("input name password sex age\n");
				printf("name>>\n");
				fgets(msg.name,32,stdin);
				msg.name[strlen(msg.name)-1]='\0';
				//scanf("%s",msg.name);

				printf("password>>\n");
				fgets(msg.password,32,stdin);
				msg.password[strlen(msg.password)-1]='\0';
				//scanf("%s",msg.password);
				strcpy(username,msg.name);

				printf("sex>>\n");
				scanf("%c",&msg.sex);
				
				printf("age>>\n");
				scanf("%d",&msg.age);
				
				send(sockfd,&msg,sizeof(msg),0);
				if(recv(sockfd,recvbuf,128,0)!=0)
				{
					if(recvbuf[0]=='0')
					{
						printf("register success\n");
					}
					else if(recvbuf[0]=='1')
					{
						printf("register error\n");
					}
					printf("%s\n",recvbuf+2);
					memset(recvbuf,0,128);
				}
				break;
			case 2:
				msg.code=2;
				printf("input name password\n");
				fgets(msg.name,32,stdin);
				msg.name[strlen(msg.name)-1]='\0';
				fgets(msg.password,32,stdin);
				msg.password[strlen(msg.password)-1]='\0';
				send(sockfd,&msg,sizeof(msg),0);
				if(recv(sockfd,recvbuf,128,0)!=0)
				{
					if(recvbuf[0]=='2')
					{
						printf("register success\n");
						choose2flag=1;
					}
					else if(recvbuf[0]=='3')
					{
						printf("register error\n");
						choose2flag=0;
					}
					printf("%s\n",recvbuf+2);
					memset(recvbuf,0,128);
					if(choose2flag==1)
					{
						goto windows2;
					}
				}
				break;
			default:
				break;
		}
	}
windows2:
	while(1)
	{
		choose2flag=0;
		printf("****************************\n");
		printf("***1.change 2.view 3.exit***\n");
		printf("***************************\n");
		scanf("%d",&choose2);
		getchar();
		switch(choose2)
		{
			case 1:
				msg.code=3;
				strcpy(msg.name,username);
				printf("plz input change password\n");
				memset(msg.password,0,32);
				fgets(msg.password,32,stdin);
				msg.password[strlen(msg.password)-1]='\0';
				send(sockfd,&msg,sizeof(msg),0);
				if(recv(sockfd,recvbuf,128,0)!=0)
				{
					if(recvbuf[0]=='4')
					{
						printf("change error\n");
					}
					else if(recvbuf[0]=='5')
					{
						printf("change success\n");
					}
					printf("%s\n",recvbuf+2);
					memset(recvbuf,0,128);
				}
				break;
			case 2:
				msg.code=4;
				
				send(sockfd,&msg,sizeof(msg),0);
				if(recv(sockfd,recvbuf,128,0)!=0)
				{
					printf("%s\n",recvbuf);
					memset(recvbuf,0,128);
				}
				break;
			case 3:
				goto windows1;
			break;
			default:
				break;
		}
	
	
	}

	return 0;
}
